// pages/qiandao/qiandao.js
let num=0
Page({
  mixins: [require('../owner/mixin/common')],
  /**
   * 页面的初始数据
   */
  data: {
    hide:false,
    num:0,//积分数
    currentDayList: '',
    currentObj:'',
    arr: ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''],
    currentDate:'',
    currentDay:'',
    year:'',
    month:'',
    currentClickKey:'',
    remindlist:[1,2,3],
    today:[],
    nowlist:[]
    },

  /**
   * 生命周期函数--监听页面加载
   */
  qiandao:function(e){
    let m =wx.getStorageSync('day')
    let num=wx.getStorageSync('num')
    console.log(m)
    var time =new Date().getDate()
    if(m!=time){
      wx.showToast({
        title: '今日已成功签到',
        duration:2000
      })
      num++
      this.setData({
        hide:false,
        num:num,
        today:time
      })
      wx.setStorageSync('day', this.data.today) 
      wx.setStorageSync('num', this.data.num)
  }else{
      wx.showToast({
        title: '今日已签到',
        icon:'error',
        duration:2000
      })
    
    }
  
  },
  onLoad: function (options) {
    let number = wx.getStorageSync('num')   
   let day = wx.getStorageSync('day')
   let n=new Date().getDate()
   console.log(number)
  //   let nowlist = this.data.nowlist
  //  nowlist.push({
  //    today:day
  //  })
   if(n!=day){
     this.setData({
      num:number,
      today:day,
       hide:true
     })
   }else{
    this.setData({
      num:number,
     today:n,
      hide:false
    })
   }
   var currentObj = this.getCurrentDayString()
    this.setData({
     currentDate: currentObj.getFullYear() + '年' + (currentObj.getMonth() + 1) + '月',
     currentDay: currentObj.getDate(),
     currentObj: currentObj,
     year: currentObj.getFullYear(),
     month: currentObj.getMonth() + 1
    })
    this.setSchedule(currentObj);
    },
    doDay: function (e) {
    this.setData({
     currentClickKey:''
    })
    var arr = this.data.arr;
    for (let i in arr) {
     var newarr = 'arr[' + i + ']';
     this.setData({
     [newarr]: ''
     })
    }
    var that = this
    var currentObj = that.data.currentObj
    var Y = currentObj.getFullYear();
    var m = currentObj.getMonth() + 1;
    var d = currentObj.getDate();
    var str = ''
    if (e.currentTarget.dataset.key == 'left') {
     m -= 1
     if (m <= 0) {
     str = (Y - 1) + '/' + 12 + '/' + d
     } else {
     str = Y + '/' + m + '/' + d
     }
    } else {
     m += 1
     if (m <= 12) {
     str = Y + '/' + m + '/' + d
     } else {
     str = (Y + 1) + '/' + 1 + '/' + d
     }
    }
    currentObj = new Date(str)
    this.setData({
     currentDate: currentObj.getFullYear() + '年' + (currentObj.getMonth() + 1) + '月',
     currentObj: currentObj,
     year: currentObj.getFullYear(),
     month: (currentObj.getMonth() + 1),
     day: ''
    })
    this.setSchedule(currentObj);
    },
    getCurrentDayString: function () {
    var objDate = this.data.currentObj
    if (objDate != '') {
     return objDate
    } else {
     var c_obj = new Date()
     var a = c_obj.getFullYear() + '/' + (c_obj.getMonth() + 1) + '/' + c_obj.getDate()
     return new Date(a)
    }
    },
    setSchedule: function (currentObj) {
    var that = this
    var m = currentObj.getMonth() + 1
    var Y = currentObj.getFullYear()
    var d = currentObj.getDate();
    var dayString = Y + '/' + m + '/' + currentObj.getDate()
    var currentDayNum = new Date(Y, m, 0).getDate()
    var currentDayWeek = currentObj.getUTCDay() + 1
    var result = currentDayWeek - (d % 7 - 1);
    var firstKey = result <= 0 ? 7 + result : result;
    var currentDayList = []
    var f = 0
    for (var i = 0; i < 42; i++) {
     let data = []
     if (i < firstKey - 1) {
     currentDayList[i] = ''
     } else {
     if (f < currentDayNum) {
      currentDayList[i] = f + 1
      f = currentDayList[i]
     } else if (f >= currentDayNum) {
      currentDayList[i] = ''
     }
     }
    }
    that.setData({
     currentDayList: currentDayList
    })
    },
    // 设置点击事件
    onClickItem: function (e) {
    // console.log(JSON.stringify(e));
    // console.log(JSON.stringify(e.currentTarget));
    this.setData({
     currentClickKey: e.currentTarget.id
    });
    },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})